{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import xlrd\n",
    "data = xlrd.open_workbook(r\"C:\\Users\\CloudCross\\Desktop\\行业人群热点明细 (带分行业场景数据)\\母婴亲子\\年轻父母.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "table = data.sheet_by_index(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[337283.0, '海泰中信广场(主楼):海泰中信主楼1F', '上海', '上海', '上海海泰中信广场(主楼):海泰中信主楼1F', '写字楼']\n"
     ]
    }
   ],
   "source": [
    "print(table.row_values(86737))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "nrows = table.nrows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sets={}\n",
    "import xlwt\n",
    "wd = xlwt.Workbook()\n",
    "for i in range(1,nrows):\n",
    "    now_row = table.row_values(i)\n",
    "    if now_row[3] in sets:\n",
    "        sets[now_row[3]].append(now_row)\n",
    "    else:\n",
    "        sets[now_row[3]]=[]\n",
    "        sets[now_row[3]].append(now_row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import xlwt\n",
    "title = (u'id',u'名称',u'省',u'市',u'地址',u'场景')\n",
    "for k in sets.keys():\n",
    "    wd = xlwt.Workbook()\n",
    "    values = sets[k]\n",
    "    del sheet\n",
    "    sheet = wd.add_sheet(values[0][2]+\"-\"+k,cell_overwrite_ok=True)\n",
    "    for i in range(len(title)):\n",
    "        sheet.write(0,i,title[i])\n",
    "    for i in range(1,len(values)+1):\n",
    "        for j in range(6):\n",
    "            sheet.write(i,j,values[i-1][j])\n",
    "    wd.save(values[0][2]+\"-\"+k+'.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import csv\n",
    "for k,v in sets.items():\n",
    "    if k!='': \n",
    "        csvfile = open(v[0][2]+\"-\"+k+'.csv', 'w',encoding='utf-8')\n",
    "    else:\n",
    "        csvfile = open('a.csv', 'w',encoding='utf-8')\n",
    "    writer = csv.writer(csvfile)\n",
    "    writer.writerows(v)\n",
    "    csvfile.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['上海', '北京']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "    for i in range(1,len(v)):\n",
    "        for j in range(len(v[i])):\n",
    "            if isinstance(v[i][j],str):\n",
    "                sheet.write(i,j,v[i][j].encode('utf-8').decode('utf-8'))\n",
    "            else:\n",
    "                sheet.write(i,j,v[i][j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import xlwt\n",
    "f = xlwt.Workbook() #创建工作簿\n",
    "sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet\n",
    "row0 = [u'业务',u'状态',u'北京',u'上海',u'广州',u'深圳',u'状态小计',u'合计']\n",
    "table.row_values(1)\n",
    "for i in range(0,len(row0)):\n",
    "    sheet1.write(0,i,row0[i])\n",
    "for i in range(0,len(table.row_values(1))):\n",
    "    sheet1.write(1,i,table.row_values(1)[i])\n",
    "f.save('demo1.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100383"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nrows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on package xlwt:\n",
      "\n",
      "NAME\n",
      "    xlwt\n",
      "\n",
      "PACKAGE CONTENTS\n",
      "    BIFFRecords\n",
      "    Bitmap\n",
      "    Cell\n",
      "    Column\n",
      "    CompoundDoc\n",
      "    ExcelFormula\n",
      "    ExcelFormulaLexer\n",
      "    ExcelFormulaParser\n",
      "    ExcelMagic\n",
      "    Formatting\n",
      "    Row\n",
      "    Style\n",
      "    UnicodeUtils\n",
      "    Utils\n",
      "    Workbook\n",
      "    Worksheet\n",
      "    antlr\n",
      "    compat\n",
      "\n",
      "DATA\n",
      "    __VERSION__ = '1.0.0'\n",
      "\n",
      "FILE\n",
      "    c:\\anaconda3\\lib\\site-packages\\xlwt\\__init__.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "help(xlwt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "242"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(sets.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[106711.0, '金华市中医院', '浙江', '金华', '浙江省金华市婺城区', '医疗健康'],\n",
       " [106920.0, '义乌?元医院荷叶塘院区', '浙江', '金华', '浙江省金华市义乌市', '医疗健康'],\n",
       " [106962.0, '义乌?元私立医院', '浙江', '金华', '浙江省金华市义乌市', '医疗健康'],\n",
       " [208946.0, '米拉烘焙时光（新安街）', '浙江', '金华', '浙江省金华市', '美食'],\n",
       " [209358.0, '好记大酒店', '浙江', '金华', '浙江省金华市', '美食'],\n",
       " [209797.0, '米拉烘焙时光（新安街）', '浙江', '金华', '浙江省金华市', '美食'],\n",
       " [209949.0, '铂爵会', '浙江', '金华', '浙江省金华市', '休闲娱乐'],\n",
       " [210487.0, '米拉烘焙时光（新安街）', '浙江', '金华', '浙江省金华市', '美食'],\n",
       " [211683.0, '', '浙江', '金华', '浙江省金华市', '美食'],\n",
       " [211882.0, '', '浙江', '金华', '浙江省金华市', '本地购物'],\n",
       " [257531.0, '', '浙江', '金华', '浙江省金华市', '本地购物']]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sets['金华']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
